home *** CD-ROM | disk | FTP | other *** search
/ CGI How-To / CGI HOW-TO.iso / chap2 / 2_9 / chk-acpt.pl < prev    next >
Encoding:
Perl Script  |  1996-06-15  |  2.0 KB  |  84 lines

  1. #!/usr/bin/perl
  2.  
  3. # Access the environment variable by name
  4.  
  5. $http_accept = $ENV{'HTTP_ACCEPT'};
  6.  
  7. # Unbuffer output so that we can see the image as it comes out,
  8. # rather than waiting for the buffer to flush.
  9.  
  10. $| = 1;
  11.  
  12. # Test the value and take the appropriate action. For the first test,
  13. # check whether the browser accepts JPEG images where the "image/jpeg"
  14. # string will be found in the HTTP_ACCEPT list and if so then send a
  15. # JPEG image to the browser.
  16.  
  17. if ($http_accept =~ m#image/jpeg#)
  18. {
  19.     # Browser understands JPEGs so send it a JPEG image
  20.     &DumpFile("lighthou.jpg", "image/jpeg");
  21. }
  22.  
  23. # Check whether the browser accepts GIF images. If the string "image/gif" is
  24. # found in the HTTP_ACCEPT list then the browser claims to support GIF images
  25. # so send it one.
  26.  
  27. elsif ($http_accept =~ m#image/gif#)
  28. {
  29.     # Browser understands GIFs so send it a GIF image
  30.     &DumpFile("lighthou.gif", "image/gif");
  31. }
  32.  
  33. # If the HTTP_ACCEPT value is not defined or neither image types (JPEG or GIF)
  34. # are supported then it will fail the first two tests. For this case perform
  35. # the default action which is to show some text.
  36.  
  37. else
  38. {
  39.     # Browser does not understand GIFs nor JPEGs so show some plain old text
  40.         print "Content-type: text/plain\n\n";
  41.     print "Text is the only thing I can show you.\n";
  42.     print "Hope you weren't expecting an image or something.\n";
  43. }
  44.  
  45. # Exit the program
  46.  
  47. exit;
  48.  
  49. sub DumpFile
  50. {
  51.     local($filename, $content_type) = @_;
  52.  
  53. # Open the specified file
  54.  
  55.     unless (open(FILE, "$filename"))
  56.     {
  57.         print "Content-type: text/plain\n\n";
  58.         print "Sorry, the file '$filename' cannot be opened.\n";
  59.         print "Please report this error to the webmaster.\n";
  60.         exit;
  61.     }
  62.  
  63. # Identify what type of file will be sent to the browser with the
  64. # MIME type from which the variable $content_type will either be
  65. # "image/jpeg "or "image/gif" depending on what the browser supports.
  66.  
  67.     print "Content-type: $content_type\n\n";
  68.  
  69. # Read and print out the entire file.
  70.  
  71.     while (<FILE>)
  72.     {
  73.         print;
  74.     }
  75.  
  76. #  Close the file and return to the main program.
  77.  
  78.     close(FILE);
  79. }
  80.  
  81. #
  82. # end of chk-acpt.pl
  83. #
  84.